//
//  themeViewController.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-2.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//
#import "themeDetailsViewController.h"
#import "MJRefresh.h"
#import "HTTPTool.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "themeViewController.h"
#import "themeCell.h"
#import "MorethemeModel.h"
#import "MBProgressHUD.h"
@interface themeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,assign)NSInteger pagecount;
@property(nonatomic,retain)NSMutableArray   * morethemeArr;
@property(nonatomic,retain)MBProgressHUD *hud;
@end

@implementation themeViewController



- (void)dealloc
{
    [_collectionView  release];
    [_morethemeArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
self.navigationItem.title=@"主题";
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width/2-30), 225);
    //设置最小的行间距
    flowLayout.minimumLineSpacing = 20;
    //设置最小的列间距
    flowLayout.minimumInteritemSpacing = 10;
    //滑动的方向,默认是垂直滑动
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 靠近屏幕边缘的距离
    //    flowLayout.sectionInset = UIEdgeInsetsMake(00, 0, 400, 0);
    
    //如果要给collectionView一个头或者尾添加一个视图,不能像tableview一样,只能写一个自定义的视图
    // 垂直滑动设定高度
    // 水平滑动设置宽度
    //    flowLayout.headerReferenceSize = CGSizeMake(0, 300);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width-40, self.view.frame.size.height-49-64-10) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    //    [self.view addSubview:self.collectionView];
[self.collectionView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.collectionView release];
    [self.collectionView registerClass:[themeCell class] forCellWithReuseIdentifier:@"themeCell"];
    //    [self.collectionView reloadData];

    [self.view addSubview:self.collectionView];
    [self createDATA];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view addSubview:self.hud];
    self.hud.labelText = @"loading...";
    // Do any additional setup after loading the view.
}

-(void)createDATA{
    
    self.morethemeArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
[manager GET:@"http://www.hunliji.com/v1/api/app/newsubjects.json?page=1&per_page=20" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSDictionary *dic = responseObject;
    NSMutableArray  *arr = [NSMutableArray array];
    arr = dic[@"subjects"];
    
    for (NSDictionary *tempdic in arr) {
        MorethemeModel *moretheme = [[MorethemeModel alloc]init];
        [moretheme setValuesForKeysWithDictionary:tempdic];
        [self.morethemeArr addObject:moretheme];
 
    }
  
    
    
    [self.collectionView reloadData];
    [self.hud hide:YES afterDelay:1];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 
    
}];
}

//添加尾部加载方法
- (void)footerRereshing
{ static NSInteger a=2;    
    NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/v1/api/app/newsubjects.json?page=%ld&per_page=20",a];
    AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
    [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSMutableArray  *arr = [NSMutableArray array];
        arr = dic[@"subjects"];
        
        for (NSDictionary *tempdic in arr) {
            MorethemeModel *moretheme = [[MorethemeModel alloc]init];
            [moretheme setValuesForKeysWithDictionary:tempdic];
            [self.morethemeArr addObject:moretheme];
            
        }

        [self.collectionView reloadData];
        
        [self.collectionView reloadData];
        [self.collectionView footerEndRefreshing];
        a++;
    }
     
        
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}




-(NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 
      return self.morethemeArr.count;

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    themeCell *myCell=[collectionView dequeueReusableCellWithReuseIdentifier:
                    @"themeCell" forIndexPath:indexPath];
    
    [myCell setBackgroundColor:[UIColor grayColor]];
  
    MorethemeModel *theme  = [[MorethemeModel alloc]init];
    theme = self.morethemeArr[indexPath.row];
    [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:theme.cover_path]];

    return  myCell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    themeDetailsViewController *themeDetailsVC = [[themeDetailsViewController alloc]init];
    [self.navigationController pushViewController:themeDetailsVC animated:YES];
    MorethemeModel *moretheme = [[MorethemeModel alloc]init];
    moretheme = self.morethemeArr[indexPath.row];
    themeDetailsVC.bid = moretheme.bid;
    themeDetailsVC.bkgimage = moretheme.bkg_path;
    themeDetailsVC.title = moretheme.title;
    themeDetailsVC.Desc = moretheme.desc;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
